<?php

namespace app\common\model\banxi\organization;

use app\common\model\common\BaseModel;
use think\Model;
use think\model\concern\SoftDelete;

/**
 * 机构用户海报
 */
class OrganizationQuotaApply extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_organization_quota_apply';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;
    protected $append = [
        'statusCn'
    ];

    public function getStatusList()
    {
        return [1 => '审核中', 2 => '审核通过', 3 => '审核拒绝'];
    }

    public function getStatusCnAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->with([
            'organizationBaseInfo' => function ($query) {
                $query->field('id,managerName,name');
            }
        ])->where($where)
            ->order(['id' => 'desc'])
            ->paginate([
                'page' => $page,
                'list_rows' => $limit
            ])->each(function ($item) {
                if (!$item['organizationBaseInfo']) {
                    $organization = new Organization();
                    $item['organizationBaseInfo'] = $organization->getOrganizationInfo();
                }
            });
    }

    public function getInfo(array $where)
    {
        $list = $this->with([
            'organizationBaseInfo' => function ($query) {
                $query->field('id,managerName,name');
            }
        ])->where($where)->find();
        if (!$list) {
            return [];
        }
        if (!$list['organizationBaseInfo']) {
            $organization = new Organization();
            $list['organizationBaseInfo'] = $organization->getOrganizationInfo();
        }
        return $list;
    }


    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------
    public function organizationBaseInfo()
    {
        return $this->hasOne(Organization::class, 'id', 'organizationId');
    }

}